<template>
  <div class="login-container" :style="{background: currentTheme}">
    <div class="theme-switch-container">
      <div class="theme-switch-btn" @click="switchTheme">
        <el-icon><Refresh /></el-icon>
      </div>
      <span class="theme-text">随机主题</span>
    </div>
    <div class="login-left">
      <div class="financial-info">
        <h1 class="app-title">智能记账系统</h1>
        <p class="app-subtitle">轻松管理您的每一笔收支</p>
        <div class="features">
          <div class="feature-item">
            <el-icon class="feature-icon"><Wallet /></el-icon>
            <span>收支明细一目了然</span>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><PieChart /></el-icon>
            <span>数据统计图表展示</span>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><TrendCharts /></el-icon>
            <span>财务趋势智能分析</span>
          </div>
        </div>
      </div>
    </div>

    <div class="login-right">
      <el-card class="login-box" shadow="always">
        <h2 class="login-title">欢迎，请先登录</h2>

        <el-form
            :model="loginForm"
            :rules="loginRules"
            ref="loginFormRef"
            @keyup.enter="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                placeholder="请输入账户名"
                size="large"
            >
              <template #prefix>
                <el-icon>
                  <UserFilled/>
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                show-password
            >
              <template #prefix>
                <el-icon>
                  <lock/>
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button
                :loading="loading"
                type="primary"
                size="large"
                @click="handleLogin"
                style="width: 100%"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>

        <div class="register-link">
          没有账号？<router-link to="/register">立即注册</router-link>
        </div>

      </el-card>
    </div>
  </div>
</template>

<script setup>
import {ref, reactive} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {UserFilled, Lock, Wallet, PieChart, TrendCharts,Refresh} from '@element-plus/icons-vue'
import {useUserStore} from '@/stores/user'
import {authApi} from '@/api/auth.js'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref()
const loading = ref(false)

// 随机选择一种主题
const themes = [
  'linear-gradient(135deg, #ff9a9e 0%, #f6d365 100%)',  // 橙粉渐变
  'linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%)',  // 橙红渐变
  'linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%)',  // 粉色渐变
  'linear-gradient(135deg, #f6d365 0%, #ffecd2 100%)',  // 黄橙渐变
  'linear-gradient(135deg, #fbc2eb 0%, #f6d365 100%)',  // 粉黄渐变
  'linear-gradient(135deg, #ff758c 0%, #ff7eb3 100%)'   // 粉红渐变
]

// 初始化主题
const currentTheme = ref(themes[0])

const loginForm = reactive({
  username: '',
  password: ''
})

// 登录表单验证规则
const loginRules = {
  username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}]
}

// 切换主题的方法
const switchTheme = () => {
  let newThemeIndex
  do {
    newThemeIndex = Math.floor(Math.random() * themes.length)
  } while (themes[newThemeIndex] === currentTheme.value && themes.length > 1)

  currentTheme.value = themes[newThemeIndex]
}

const handleLogin = async () => {
  const valid = await loginFormRef.value.validate().catch(() => false)
  if (!valid) return
  loading.value = true
  try {
    const response = await authApi.login(loginForm)
    if (response.code === 200) {
      userStore.setToken(response.data.token)
      userStore.setUserInfo(response.data.user)
      ElMessage.success('登录成功！')
      await router.push({path: '/home'})
    } else {
      ElMessage.error(response.message || '登录失败，请检查用户名和密码')
    }
  } catch (error) {
    console.error('Login error:', error)
    ElMessage.error('网络请求失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>

.theme-switch-container {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 10;
}

.theme-switch-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 5px;
}

.theme-switch-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
  transform: rotate(90deg);
}

.theme-switch-btn .el-icon {
  font-size: 20px;
  color: white;
}

.theme-text {
  color: white;
  font-size: 12px;
  text-align: center;
  white-space: nowrap;
}

.login-container {
  min-height: 100vh;
  display: flex;
  /*background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);*/
  /*background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);*/
  /*background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);*/
  background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);

}

.login-left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  padding: 20px;
}

@media (max-width: 992px) {
  .login-left {
    display: none;
  }

  .login-container {
    justify-content: center;
  }
}

.financial-info {
  max-width: 500px;
  text-align: center;
}

.app-title {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  font-weight: bold;
}

.app-subtitle {
  font-size: 1.2rem;
  margin-bottom: 3rem;
  opacity: 0.9;
}

.features {
  text-align: left;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 1.5rem;
  font-size: 1.1rem;
  transition: transform 0.3s ease; /* 添加过渡动画 */
}

/* 为每个 feature-item 添加悬停效果 */
.feature-item:hover {
  transform: translateY(-5px); /* 单个项向上浮动5px */
  cursor: default;
}

.feature-icon {
  font-size: 1.5rem;
  margin-right: 15px;
  color: #ffd700;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #667eea;
  font-size: 1.8rem;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}


</style>
